package com.share.module.sys.role.web;

import com.share.module.sys.role.entity.Role;
import com.share.module.sys.role.service.RoleService;
import com.share.module.sys.role.vo.RolePageQueryVO;
import com.share.module.sys.role.vo.RolePageVO;
import com.stock.common.response.ApiResult;
import com.stock.mybatisplus.utils.PageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author lijy
 * @since 2023-04-17
 */
@Tag(name = "角色管理", description = "角色增删改查及角色菜单绑定接口")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @Operation(summary = "分页查询", description = "分页查询角色列表")
    @GetMapping("/page")
    public ApiResult<PageUtils<RolePageVO>> findPage(@Parameter(description = "分页查询条件") RolePageQueryVO pageQueryVO) {
        PageUtils<RolePageVO> page = roleService.page(pageQueryVO);
        return ApiResult.success(page);
    }

    @Operation(summary = "保存或更新", description = "保存或更新角色信息")
    @PostMapping
    public ApiResult<Boolean> save(@Parameter(description = "角色实体") @RequestBody Role role) {
        return ApiResult.success(roleService.saveOrUpdate(role));
    }

    @Operation(summary = "批量删除", description = "批量删除角色")
    @PostMapping("/del/batch")
    public ApiResult<Boolean> batchDel(@Parameter(description = "角色ID列表") @RequestBody List<Integer> ids) {
        return ApiResult.success(roleService.removeByIds(ids));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除", description = "根据id删除角色")
    public ApiResult<?> delete(@Parameter(description = "角色ID") @PathVariable Integer id) {
        return ApiResult.success(roleService.removeById(id));
    }

    @GetMapping
    @Operation(summary = "列表数据", description = "查询所有角色列表")
    public ApiResult<?> findAll() {
        return ApiResult.success(roleService.list());
    }

    @GetMapping("/{id}")
    @Operation(summary = "查找", description = "根据id查找角色")
    public ApiResult<?> findOne(@Parameter(description = "角色ID") @PathVariable Integer id) {
        return ApiResult.success(roleService.getById(id));
    }

    /**
     * 绑定角色和菜单关系
     * @param roleId 角色id
     * @param menuId 菜单id数组
     * @return
     */
    @Operation(summary = "绑定角色和菜单关系", description = "设置角色可访问的菜单列表")
    @PostMapping("/roleMenu/{roleId}")
    public ApiResult<?> roleMenu(@Parameter(description = "角色ID") @PathVariable Integer roleId,
                                 @Parameter(description = "菜单ID列表") @RequestBody List<Integer> menuId) {
        roleService.setRoleMenu(roleId, menuId);
        return ApiResult.success();
    }

    @Operation(summary = "绑定角色id查找菜单id", description = "根据角色ID获取绑定的菜单ID列表")
    @GetMapping("/roleMenu/{roleId}")
    public ApiResult<List<Integer>> getRoleMenu(@Parameter(description = "角色ID") @PathVariable Integer roleId) {
        return ApiResult.success(roleService.getRoleMenu(roleId));
    }

}

